<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class View extends Object{
    
    var $__passedVars = array(
    'viewVars', 'action', 'base', 
    'here', 'name', 'viewPath',
    'params', 'data', 'passedArgs', 'autoLayout'
    );
    
    var $hasRendered = false;
	var $base = null;
	var $here = null;    
    var $viewPath = null ;
	var $name = null;    
	var $params = array();
	var $data = array();    
    var $viewVars = array();
    var $action = null;
    var $autoLayout = true;
    
    function __construct(&$controller) {
        if (is_object($controller)) {
            $count = count($this->__passedVars);
            for ($j = 0; $j < $count; $j++) {
                $var = $this->__passedVars[$j];
                $this->{$var} = $controller->{$var};
            }
        }
        parent::__construct();


    }    
    
    function render($action = null, $layout = null){
        if ($this->hasRendered) {
				return true;
			}
			$out = null;

//			if ($file != null) {
//				$action = $file;
//			}

			if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
				$out = $this->_render($viewFileName, $this->viewVars);
			}
            //echo $viewFileName;
            if ($out !== FALSE){
                if ($this->autoLayout){
                    $out = $this->renderLayout($out, $layout);
                }
                $this->hasRendered = true;
            }else{
                return 'Error';
            }
            
            return $out;


    }
    
    function _getViewFileName($name = null){
//			$subDir = null;
//
//			if (!is_null($this->subDir)) {
//				$subDir = $this->subDir . DS;
//			}

			if ($name === null) {
				$name = $this->action;
			}
			$name = str_replace('/', DS, $name);
            
			if (strpos($name, DS) === false && $name[0] !== '.') {
				$name = $this->viewPath . $name . '.php';
            }

            if (file_exists($name)){
                return $name;
            }
            return $name;
    }
    
    function _render($___viewFn, $___dataForView){
        extract($___dataForView, EXTR_SKIP);
        
        ob_start();
        include ($___viewFn);
        $out = ob_get_clean();

        return $out;
    }
    
    function renderLayout($content_for_layout, $layout = null){
        if ($layout !== null || !file_exists($layout.'.php')){
            $layoutFileName = 'template.php';
        }
        $dataForLayout = array_merge($this->viewVars, array(
        'content_for_layout' => $content_for_layout,
        ));
        if (!isset ($dataForLayout['title_for_layout'])){
            $dataForLayout['title_for_layout'] = null;
        }
        $this->output = $this->_render($layoutFileName, $dataForLayout);
        
        return $this->output;
        
    }
    
    function formatdate(& $date){
        if (isset ($date))
            return implode("/", array_reverse(explode("-", $date)));
        else
            return NULL;
        
    }
    
    function verifica(& $param) {
        return (isset ($param) ? $param : NULL );
    }
}
?>
